<?php

/**
 * 列出你的理财信息
 * 
 * 贷款总额：7386，分期：12，月利率：0.0037
    理财年化：0.082
    总利息：327.84
    总利润：330.30
 */
public function actionGoMoney(){
    $month = 12;//分多少期还
    $money = 7386;//贷多少钱
    $moneyEnd = $money;
    $monthRate = 0.0037;//贷款月利率
    $yearRate = 0.082;//理财年化率
    $profitsTotal = 0;//总利润
    $calculatorArr = $this->_loansInfo($money, $month, $monthRate, $yearRate);
    for ($i=1;$i<=$month;$i++){
        $liabilitiesMonth = $calculatorArr['liabilitiesMonth'];//每月应还
	    $profitsCur = $this->_curProfit($moneyEnd, $yearRate);//当月利润
	    $profitsTotal= bcadd($profitsTotal, $profitsCur);
	    $moneyEnd = bcsub($moneyEnd, $liabilitiesMonth); // 月剩款=剩款-月应还
	    $moneyEnd = bcadd($profitsCur, $moneyEnd);// 每月剩款 = 月剩款+ 加上每月利润
    }
    
    echo '<pre>';
    echo "贷款总额：{$money}，分期：{$month}，月利率：{$monthRate}<br>";
    echo "理财年化：{$yearRate}<br>";
    echo "总利息：{$calculatorArr['interestTotal']}<br>";
    echo "总利润：{$profitsTotal}";
}

/**
 * 贷款信息
 * @param double $money 贷多少钱
 * @param int $month 分多少期还
 * @param float $monthRate 贷款月利率
 * @return array 
 */
private function _loansInfo($money, $month, $monthRate){
    bcscale(2);
    $interest = bcmul($money, $monthRate);//月还款利息=$money*$monthRate
    $interestTotal = bcmul($month, $interest);//总利息
    $liabilitiesTotal = bcadd($money, $interestTotal);//总负债金额 83468.00
    $liabilitiesMonth = bcdiv($liabilitiesTotal, $month);//每月应还款 3477.83
    return [
        'liabilitiesMonth'=>$liabilitiesMonth,
        'liabilitiesTotal'=>$liabilitiesTotal,
        'interestTotal'=>$interestTotal,
    ];
}

/**
 * 当月利润
 * @param unknown $money 当月投入本金
 * @param unknown $rate 年化收益率
 * @return string 每月利润
 */
private function _curProfit($money, $rate){
    bcscale(2);
    $profitYear = bcmul($money, $rate);//年利润
    $profitDay = bcdiv($profitYear, 360);// 每日利润
    return bcmul($profitDay, 30);
}